package com.suoluo.zenadmin.oss;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.lf5.util.StreamUtils;
import org.springframework.web.multipart.MultipartFile;

import com.aliyun.openservices.ClientConfiguration;
import com.aliyun.openservices.ClientException;
import com.aliyun.openservices.oss.OSSClient;
import com.aliyun.openservices.oss.OSSException;
import com.aliyun.openservices.oss.model.ListObjectsRequest;
import com.aliyun.openservices.oss.model.OSSObject;
import com.aliyun.openservices.oss.model.OSSObjectSummary;
import com.aliyun.openservices.oss.model.ObjectListing;
import com.aliyun.openservices.oss.model.ObjectMetadata;
import com.suoluo.zenadmin.common.StringUtil;
import com.suoluo.zenadmin.common.config.SystemConfigHolder;
import com.suoluo.zenadmin.common.encrypt.EncryptUtil;
import com.suoluo.zenadmin.common.file.ImageUtils;

public class OSSServiceImpl implements OSSService{


	private static final Log logger = LogFactory.getLog(OSSServiceImpl.class);
	
	private OSSClient client;
	
	private final Object lock = new Object();

	private OSSClient createOSSClient(){
		return createOSSClient(!SystemConfigHolder.isProduction());
	}
	
	private OSSClient createOSSClient(boolean debug){
		synchronized (lock) {
			if (client ==  null){
				ClientConfiguration conf = new ClientConfiguration();
				// 设置HTTP最大连接数为10
				conf.setMaxConnections(10);
				// 设置TCP连接超时为5000毫秒
				conf.setConnectionTimeout(5000);
				// 设置最大的重试次数为3
				conf.setMaxErrorRetry(3);
				// 设置Socket传输数据超时的时间为2000毫秒
				conf.setSocketTimeout(2000);
//				青岛节点外网地址： oss-cn-qingdao.aliyuncs.com 
//				青岛节点内网地址： oss-cn-qingdao-internal.aliyuncs.com
//				杭州节点外网地址： oss-cn-hangzhou.aliyuncs.com
//				杭州节点内网地址： oss-cn-hangzhou-internal.aliyuncs.com 
//				原地址oss.aliyuncs.com 默认指向杭州节点外网地址。
//				原内网地址oss-internal.aliyuncs.com 默认指向杭州节点内网地址。
				String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
				//在生产环境中使用内网
				if (!debug){
					endpoint = "http://oss-cn-hangzhou-internal.aliyuncs.com";
				}
				client = new OSSClient(endpoint,"OGl29AQHAfIWMslo", "x2oIYNt6zm0UFQr1LcyFrd7WZJSP3g",conf);
			}
			return client;
		}
	}
	
	private String getImageBucketName(){
		return getImageBucketName (!SystemConfigHolder.isProduction());
	}
	
	private String getImageBucketName(boolean debug){
		if (!debug){
			return "zentravel-img";
		}
		return "zentravel-dev";
	}
	

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String putImage(File file) {
		try {
			String fileName = file.getName();
			String contentType = checkImageType(new FileInputStream(file), fileName);
//			ImageName imageName = buildImageName(fileName);
			String key = buildImageKey(fileName);
			return doPutImage(new FileInputStream(file), key, file.length(), contentType);
		} catch (FileNotFoundException e) {
			throw new OSSImageException(e.getMessage(), e);
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String putImage(String filePath) {
		return putImage(new File(filePath));
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String putImage(MultipartFile file) {
		if (file == null || file.isEmpty()){
			throw new OSSImageException("multifile is null");
		}
		String fileName = file.getOriginalFilename();
		try {
			String contentType = checkImageType(file.getInputStream(), fileName);
//			ImageName imageName = buildImageName(fileName);
			String key = buildImageKey(fileName);
			return doPutImage(file.getInputStream(), key, file.getSize(), contentType);
		} catch (IOException e) {
			throw handleException(e);
		}
	}
	
	@Override
	public String putTempImage(MultipartFile file) {
		if (file == null || file.isEmpty()){
			throw new OSSImageException("multifile is null");
		}
		String fileName = file.getOriginalFilename();
		try {
			String contentType = checkImageType(file.getInputStream(), fileName);
			String key = buildTempImageKey(fileName);
			return doPutImage(file.getInputStream(), key, file.getSize(), contentType);
		} catch (IOException e) {
			throw handleException(e);
		}
	}
	
	
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public List<String> putImages(MultipartFile[] files) {
		if (files == null || files.length == 0){
			throw new OSSImageException("maultipart files is null");
		}
		List<String> names = new ArrayList<String>();
		try{
			for (MultipartFile file : files){
				//如果表单域没有选择文件或者选择的文件是空，则过滤掉
				if (file == null || file.isEmpty()){
					continue;
				}
				String name = putImage(file);
				names.add(name);
			}
			return names;
		}catch(Exception e){
			//1.需要删除
			deleteImages(names);
			//处理异常
			throw handleException(e);
		}
	}

	private String doPutImage(InputStream is, String key, long length,String contentType){
		OSSClient client = createOSSClient();
	    // 上传Object.
	    String bucketName = getImageBucketName();
//	    ImageName imageName = buildImageName(fileName);
	    //保存原始图片
	    try{
	    	putObject(client, bucketName, is, key, length,contentType);
	    	return key;
	    }catch(Exception e){
	    	throw handleException(e);
	    }
	}
//	/**
//	 * {@inheritDoc}
//	 */
//	@Override
//	public ImageName putAndCompressImage(File file) {
//		try {
//			String fileName = file.getName();
//			//检查文件类型
//			String imageContentType = checkImageType(new FileInputStream(file), fileName);
//			InputStream origInput = new FileInputStream(file);
//			InputStream compressInput = new FileInputStream(file);
//			ImageName imageName = buildImageName(fileName);
//			return doPutAndCompressImage(origInput, compressInput,imageName, file.length(), imageContentType);
//		} catch (FileNotFoundException e) {
//			throw new OSSImageException(e.getMessage(),e);
//		}
//	}

//	/**
//	 * {@inheritDoc}
//	 */
//	@Override
//	public ImageName putAndCompressImage(String filePath) {
//		File file = new File(filePath);
//		return putAndCompressImage(file);
//	}

//	/**
//	 * {@inheritDoc}
//	 */
//	@Override
//	public ImageName putAndCompressImage(MultipartFile file) {
//		if (file == null || file.isEmpty()){
//			throw new OSSImageException("the uploaded file is empty, that is, either no file has been chosen in the multipart form or the chosen file has no content.");
//		}
//		try {
//			String imageContentType = checkImageType(file.getInputStream(), file.getOriginalFilename());
//			//流会自动关闭
//			ImageName imageName = buildImageName(file.getOriginalFilename());
//			return doPutAndCompressImage(file.getInputStream(), file.getInputStream(),imageName, file.getSize(), imageContentType);
//		} catch (IOException e) {
//			throw handleException(e);
//		}
//	}
	
	private String checkImageType(InputStream input,String imageName){
		try{
			String imageContentType = ImageUtils.getImageType(input);
			if (!ImageUtils.isValidImageType(imageContentType)){
				throw new OSSImageException("image type invalid,must be JPG|PNG|GIF,["+imageName+"]");
			}
			return imageContentType;
		}finally{
			IOUtils.closeQuietly(input);
		}
		
	}
	
//	/**
//	 * {@inheritDoc}
//	 */
//	@Override
//	public List<ImageName> putAndCompressImage(MultipartFile[] files) {
//		if (files == null || files.length == 0){
//			throw new OSSImageException("maultipart files is null");
//		}
//		List<ImageName> names = new ArrayList<OSSService.ImageName>();
//		try{
//			for (MultipartFile file : files){
//				//如果表单域没有选择文件或者选择的文件是空，则过滤掉
//				if (file == null || file.isEmpty()){
//					continue;
//				}
//				ImageName name = putAndCompressImage(file);
//				names.add(name);
//			}
//			return names;
//		}catch(Exception e){
//			//1.需要删除
////			deleteImages(names);
//			//处理异常
//			throw handleException(e);
//		}
//	}
	
//	private ImageName doPutAndCompressImage(InputStream origInput,InputStream compressInput,String fileName,long length,String contentType) {
//		 // 上传Object.
//	    ImageName imageName = buildImageName(fileName);
//	    doPutAndCompressImage(origInput, compressInput, imageName, length, contentType);
//	    //保存原始图片
//	    return imageName;
////	    doPutImage(origInput, imageName.getOrigName(), length,contentType);
//////	    InputStream objectContent = null;
////	    try{
//////	    	objectContent = readImage(imageName.getOrigName());
////	    	int width = SystemConfigHolder.getSystemImgBlockWidth();
////			int height = SystemConfigHolder.getSystemImgBlockHeight();
////			byte[] data = compressImage(compressInput, height, width, true);
////			ByteArrayInputStream compressIs = new ByteArrayInputStream(data);
////		    //保存压缩的图片
////			doPutImage(compressIs, imageName.getMiniName(), data.length,contentType);
////	    	return imageName;
////	    }catch (Exception e) {
////	    	//删除原始的图片
////	    	deleteImage(imageName.getOrigName());
////	    	throw handleException(e);
////		}
//	}
	
//	private ImageName doPutAndCompressImage(InputStream origInput,InputStream compressInput,ImageName imageName,long length,String contentType) {
//		 // 上传Object.
////	    ImageName imageName = buildImageName(fileName);
//	    //保存原始图片
//	    doPutImage(origInput, imageName.getOrigName(), length,contentType);
////	    InputStream objectContent = null;
//	    try{
////	    	objectContent = readImage(imageName.getOrigName());
//	    	int width = SystemConfigHolder.getSystemImgBlockWidth();
//			int height = SystemConfigHolder.getSystemImgBlockHeight();
//			byte[] data = compressImage(compressInput, height, width, true);
//			ByteArrayInputStream compressIs = new ByteArrayInputStream(data);
//		    //保存压缩的图片
//			doPutImage(compressIs, imageName.getMiniName(), data.length,contentType);
//	    	return imageName;
//	    }catch (Exception e) {
//	    	//删除原始的图片
//	    	deleteImage(imageName.getOrigName());
//	    	throw handleException(e);
//		}
//	}
	

	
//	private ImageName buildImageName(String fileName){
//		String fileSuffix = FileUtils.parseFileNameSuffix(fileName);
//		//名称加上系统的时间数，避免产生重复的名称
//		String md5Name = EncryptUtil.md5(fileName+System.nanoTime())+fileSuffix;
//		ImageName imageName = new ImageName("web/"+md5Name);
//		imageName.setMiniName("web/mini/"+md5Name);
//		return imageName;
//	}
	
	private String buildImageKey(String fileName){
//		String fileSuffix = FileUtils.parseFileNameSuffix(fileName);
		//名称加上系统的时间数，避免产生重复的名称
		String md5Name = EncryptUtil.md5(fileName+System.nanoTime());
		return "web/"+md5Name;
//		return md5Name;
	}
	
	private String buildTempImageKey(String fileName){
		long mills = System.currentTimeMillis();
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd-");
		
		
//		String md5Name = EncryptUtil.md5("ssss"+mills);
//		String name =  "temp/"+format.format(new Date(mills)) +md5Name;
//		System.out.println(name);
		String md5Name = EncryptUtil.md5(fileName+mills);
		return "temp/"+format.format(new Date(mills))+md5Name;
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void deleteImage(String key) {
		if (StringUtil.isEmpty(key)){
			return;
		}
		// 初始化OSSClient
	    OSSClient client = createOSSClient();
	    String bucketName = getImageBucketName();
	    try{
	    	// 删除Object
		    client.deleteObject(bucketName, key);
		    if (logger.isInfoEnabled()){
	    		logger.info("deleted image["+key+"]");
	    	}
	    }catch(Exception e){
	    	throw handleException(e);
	    }
	}
//	/**
//	 * {@inheritDoc}
//	 */
//	@Override
//	public void deleteImage(ImageName imageName) {
//		if (imageName == null){
//			return;
//		}
//		//1.验证是否有小图片
//		if (!StringUtil.isEmpty(imageName.getMiniName())){
//			deleteImage(imageName.getMiniName());
//		}
//		//删除原始图片
//		deleteImage(imageName.getOrigName());
//	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void deleteImages(List<String> names) {
		if (names == null || names.isEmpty()){
			return;
		}
		for (String name : names){
			deleteImage(name);
		}
	}
//	/**
//	 * {@inheritDoc}
//	 */
//	@Override
//	public ImageName updateImage(String filePath, ImageName oldImageName) {
//		File file = new File(filePath);
//		return updateImage(file, oldImageName);
//	}

//	@Override
//	public ImageName updateImage(File file, ImageName oldImageName) {
//		try {
//			return doUpdateImage(new FileInputStream(file), new FileInputStream(file),
//					new FileInputStream(file), file.getName(),
//					file.length(), oldImageName);
//		} catch (FileNotFoundException e) {
//			throw handleException(e);
//		}
//	}
	
	
//	@Override
//	public ImageName updateImage(MultipartFile file, ImageName oldImageName){
//		if (file == null || file.isEmpty()) {
//			throw new OSSImageException(
//					"the uploaded file is empty, that is, either no file has been chosen in the multipart form or the chosen file has no content.");
//		}
//		try {
//			return doUpdateImage(file.getInputStream(), file.getInputStream(),
//					file.getInputStream(), file.getOriginalFilename(),
//					file.getSize(), oldImageName);
//		} catch (IOException e) {
//			throw handleException(e);
//		}
//	}
//	
	@Override
	public String updateImage(MultipartFile file, String oldImageName) {
		if (file == null || file.isEmpty()) {
			throw new OSSImageException(
					"the uploaded file is empty, that is, either no file has been chosen in the multipart form or the chosen file has no content.");
		}
		
//		String newImageName = null;
		try {
			String contentType = checkImageType(file.getInputStream(), oldImageName);
			if (logger.isInfoEnabled()){
				logger.info("update image[origImg="+oldImageName+"]");
			}
			if (StringUtil.isEmpty(oldImageName)){
				oldImageName = buildImageKey(file.getOriginalFilename());
			}
			
//			newImageName = putImage(file);
//			doPutImage(is, key, length, contentType)
			
			doPutImage(file.getInputStream(), oldImageName, file.getSize(), contentType);
			// 2.然后删除旧图片
//			deleteImage(oldImageName);
			return oldImageName;
		} catch (Exception e) {
			// 异常删除新的图片
//			deleteImage(newImageName);
			throw handleException(e);
		}
	}
	
//	private ImageName doUpdateImage(InputStream validInput,
//			InputStream origInput, InputStream minInput, String fileName,
//			long length, ImageName oldImageName) {
//		String origImg = oldImageName.getOrigName();
//		String miniImg = oldImageName.getMiniName();
//		if (!StringUtil.isEmpty(origImg)
//				&& StringUtil.isEmpty(miniImg) || !StringUtil.isEmpty(miniImg) && StringUtil.isEmpty(origImg)){
//			throw new OSSImageException("old image name invalid,[origImg="+origImg+",miniImg="+miniImg+"]");
//		}
//		if (logger.isInfoEnabled()){
//			logger.info("update image[origImg="+origImg +",miniImg="+miniImg+"]");
//		}
//		// 1.保存新图片
//		// ImageName newImageName = null;
//		// try {
//		// String fileName = file.getOriginalFilename();
//		String imageContentType = checkImageType(validInput, fileName);
//
//		// 验证老的图片的名字是否存在,如果老的原始图片和小图片都不存在则需要生成新的名称
//		if (StringUtil.isEmpty(origImg)
//				&& StringUtil.isEmpty(miniImg)) {
//			oldImageName = buildImageName(fileName);
//		}
//
//		
//		doPutAndCompressImage(origInput, minInput, oldImageName, length,
//				imageContentType);
//		// 2.然后删除旧图片
//		// deleteImage(oldImageName);
//		return oldImageName;
//		// } catch (Exception e) {
//		// // 异常删除新的图片
//		// // deleteImage(newImageName);
//		// throw handleException(e);
//		// }
//	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public String copyImage(String key, String destImageName) {
//		String destKeyName = EncryptUtil.md5(destImageName+System.nanoTime());
		String destKey =  buildImageKey(destImageName);
		return doCopyImage(key, destKey);
	}
	
//	/**
//	 * {@inheritDoc}
//	 */
//	@Override
//	public ImageName copyImage(ImageName key, String destImageName) {
//		String destKeyName = EncryptUtil.md5(destImageName+System.nanoTime());
//		String origImageKey = null;
//		
//		try{
//			origImageKey = doCopyImage(key.getOrigName(), destKeyName);
//			String miniImageKey =  doCopyImage(key.getMiniName(), destKeyName);
//			return new ImageName(origImageKey, miniImageKey);
//		}catch(Exception e){
//			deleteImage(origImageKey);
//			throw handleException(e);
//		}
//	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public byte[] readImage(String key) {
		OSSClient client = createOSSClient();
		String bucketName = getImageBucketName();
		// 获取Object，返回结果为OSSObject对象
		OSSObject object = client.getObject(bucketName, key);
		// 获取ObjectMeta
		// ObjectMetadata meta = object.getObjectMetadata();

		// 获取Object的输入流
		InputStream input = object.getObjectContent();
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		try {
			StreamUtils.copyThenClose(input, output);
			return output.toByteArray();
		} catch (IOException e) {
			throw handleException(e);
		}
	}
	
	private String doCopyImage(String srcKey, String destKey){
		if (StringUtil.isEmpty(srcKey)){
			throw new OSSImageException("src key is null");
		}
		OSSClient client = createOSSClient();
//		//从源有的key重解析出图片的后缀
//		String fileSuffix = FileUtils.parseFileNameSuffix(srcKey);
		//需要支持同级目录的拷贝，所以需要解析出目录
//		String dir = "";
//		int index = srcKey.lastIndexOf("/");
//		if (index >= 0){
//			dir = srcKey.substring(0,index + 1);
//		}
		
		//生成目标的key
//		String destKey = dir + destKeyName + fileSuffix;
		
		String imageBucketName = getImageBucketName();
//		web/mini/178f5f32269f54e12d9898a4bcd1785c.jpg
		// 拷贝Object
		try{
			client.copyObject(imageBucketName, srcKey, imageBucketName, destKey);
			if (logger.isInfoEnabled()){
				logger.info("copy image from " + srcKey +" to " + destKey);
			}
			return destKey;
		}catch(Exception e){
			throw handleException(e);
		}
	}
	

	
	private OSSImageException handleException(Exception e){
		/**
		 * // ClientException // //
		 * ClientException指SDK内部出现的异常，比如未设置BucketName，网络无法到达等等。 // // //
		 * OSSException // //
		 * OSSException指服务器端错误，它来自于对服务器错误信息的解析。OSSException一般有以下几个成员： // •Code：
		 * OSS返回给用户的错误码。 // •Message： OSS给出的详细错误信息。 // •RequestId：
		 * 用于唯一标识该次请求的UUID；当你无法解决问题时，可以凭这个RequestId来请求OSS开发工程师的帮助。 // •HostId：
		 * 用于标识访问的OSS集群（目前统一为oss.aliyuncs.com）
		 */
		if (e instanceof ClientException){
			logger.error("",e);
			return new OSSImageException("network unavailable or bucket not exsits", e);
		}else if (e instanceof OSSException){
			OSSException osse = (OSSException)e;
			logger.error("[code="+osse.getErrorCode()+",message="+osse.getMessage()+",requestId="+osse.getRequestId()+",hostId="+osse.getHostId()+"]",osse);
			return new OSSImageException(osse.getMessage(),osse);
		}else if (e instanceof OSSImageException){
			return (OSSImageException)e;
		}else{
			logger.error("",e);
			return new OSSImageException("unkonw error", e);
		}
	}


//	private byte[] compressImage(InputStream is,int height, int width, boolean proportion){
//		try {
//			Image img = ImageIO.read(is);
//			// 判断图片的格式是否正确
//			if (img.getWidth(null) == -1) {
//				throw new RuntimeException("invalid image type");
//			}
//			int newWidth = width;
//			int newHeight = height;
//			// 是否是等比缩放
//			if (proportion) {
//				// 为等比缩放计算输出的图片宽度及高度
//				double widthRate = (img.getWidth(null))
//						/ (double) width;
//				double heightRate = (img.getHeight(null))
//						/ (double) height;
//				// 根据缩放比率大的进行缩放控制
//				double rate = Math.min(widthRate, heightRate);
//				newWidth = (int) ((img.getWidth(null)) / rate);
//				newHeight = (int) ((img.getHeight(null)) / rate);
//			}
//
//			BufferedImage tag = new BufferedImage(newWidth, newHeight,
//					BufferedImage.TYPE_INT_RGB);
//			
//			/*
//			 * Image.SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢
//			 */
//			Graphics2D g = tag.createGraphics();
//			g.drawImage(img.getScaledInstance(newWidth, newHeight,
//					Image.SCALE_SMOOTH), 0, 0, null);
//			g.dispose();
////			File parentDir = outputFile.getParentFile();
////			// 如果输出的目录不存在，则需要创建
////			if (!parentDir.exists()) {
////				parentDir.mkdirs();
////			}
////			out = new FileOutputStream(outputFile);
//			// JPEGImageEncoder可适用于其他图片类型的转换
//			ByteArrayOutputStream dest = new ByteArrayOutputStream();
//			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(dest);
//			encoder.encode(tag);
//			return dest.toByteArray();
//		} catch (IOException e) {
//			throw new OSSImageException("fail to compress image", e);
//		} 
//	}
	
	private String putObject(OSSClient client,String bucketName,InputStream is,String key,long length,String contentType){
		 // 创建上传Object的Metadata
	    ObjectMetadata meta = new ObjectMetadata();
	    // 必须设置ContentLength
	    meta.setContentLength(length);
	    meta.setContentType(contentType);
	    // 上传Object.
	    client.putObject(bucketName, key, is, meta);
	    if (logger.isInfoEnabled()){
    		logger.info("uploaded object["+key+"]");
    	}
	    return key;
	}
	
	public void bakImageToBucket(String bucketName){
		OSSClient client = createOSSClient(true);
		String origBucket = getImageBucketName(true);
		
//		listRequest.setMarker(marker);
		String marker = null;
		int size = 0;
		do {
			ListObjectsRequest listRequest = new ListObjectsRequest(origBucket);
			listRequest.setMarker( marker);
//			listRequest.setMaxKeys(50);
			ObjectListing objectlist = client.listObjects(listRequest);
			List<OSSObjectSummary> objects = objectlist.getObjectSummaries();
			for (OSSObjectSummary object : objects){
				String key = object.getKey();
//				client.copyObject(origBucket, key, bucketName, key);
			}
			
			System.out.println(objects.size());
			size += objects.size();
			marker = objectlist.getNextMarker();
		} while (!StringUtil.isEmpty(marker));
		
		System.out.println(size);
//		for (String key : objects.getCommonPrefixes()){
//			
//		}
//		 objects.getMarker();
//		CopyObjectRequest copyRequest = new CopyObjectRequest(sourceBucketName, sourceKey, destinationBucketName, destinationKey)
//		client.copyObject(copyObjectRequest)
	}
	
	
	public static void main(String [] args){
		OSSServiceImpl ossService = new OSSServiceImpl();
		OSSClient client = ossService.createOSSClient(true);
		String bucketName = ossService.getImageBucketName(true);
		
//		String key = "http://images.zentravel.cn/web/b7ebffbfa6267cf74f16d6f5df6b6266.jpg@!small";
		String key = "http://images.zentravel.cn/web/b7ebffbfa6267cf74f16d6f5df6b6266.jpg";
		OSSObject object = client.getObject(bucketName, key);
		
		// 获取Object的输入流
		InputStream input = object.getObjectContent();
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		try {
			StreamUtils.copyThenClose(input, output);
			byte [] imageData = output.toByteArray();
		} catch (IOException e) {
//			throw handleException(e);
		}
//		ossService.bakImageToBucket(null);
	}
}
